<?php
namespace lib\http;
/*class Session extends \SessionHandler
{
    private  $redis;
    public function __construct()
    {
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1', 6379);
        $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
    }

    public function __destruct()
    {
        $this->redis->close();
    }

    public function close()
    {
        file_put_contents(__DIR__.'/s.txt',"close\r\n",FILE_APPEND);
        return true;
    }

    public function create_sid()
    {
        return parent::create_sid();
    }

    public function destroy ($session_id)
    {
        $this->redis->delete('sess_'.$session_id);
        file_put_contents(__DIR__.'/s.txt',"destroy\r\n",FILE_APPEND);
        return true;
    }

    public function gc($maxlifetime)
    {
        return true;
    }

    public function open($save_path , $session_name)
    {
        file_put_contents(__DIR__.'/s.txt',"open\r\n",FILE_APPEND);
        return true;
    }

    public function read($session_id)
    {
        $str=$this->redis->get('sess_'.$session_id);
        if(!$str) $str='';

        file_put_contents(__DIR__.'/s.txt',"read\r\n",FILE_APPEND);
        return $str;
    }

    public function write($session_id,$session_data)
    {
        $this->redis->set('sess_'.$session_id,$session_data,3600);

        file_put_contents(__DIR__.'/s.txt',"write\r\n",FILE_APPEND);
        return true;
    }
}*/
class Session extends SessionHandler
{
    private $redis;
    public function __construct()
    {
        $this->redis=new Redis();
        $this->redis->connect('127.0.0.1',6379);
        $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
        $this->redis->select(0);
    }

    public function read($session_id)
    {
        $session_id='sess_'.$session_id;
        $str=$this->redis->get($session_id);
        if (!$str)$str='';
        return $str;

    }

    public function write($session_id, $session_data)
    {
        $session_id='sess_'.$session_id;
        $this->redis->set($session_id,$session_data,3600);
        return true;
    }
    public function destroy($session_id)
    {
        $session_id='sess_'.$session_id;
        $bool=$this->redis->del($session_id);
        if ($bool)return true;
        return false;
    }

    public function __destruct()
    {
        $this->redis->close();
        // TODO: Implement __destruct() method.
    }
}
$session=new Session();
$bool=session_set_save_handler($session);
//var_export($bool);
session_start();
//$_SESSION['php']=111;
//echo 'ok';
//echo $_SESSION['php'];
session_destroy();
//echo 'ok';